package com.udaos.modules.card.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.udaos.common.base.Result;
import com.udaos.common.page.DataTableColumn;
import com.udaos.common.page.DataTableOrder;
import com.udaos.models.card.Ud_card;
import com.udaos.models.product.Ud_product_type;
import com.udaos.models.region.Ud_region;
import com.udaos.models.school.Ud_school;
import com.udaos.models.sys.Sys_user;
import com.udaos.modules.card.services.UdCardService;
import com.udaos.modules.company.services.UdCompanyService;
import com.udaos.modules.product.services.UdProductTypeService;
import com.udaos.modules.region.services.UdRegionService;
import com.udaos.modules.school.services.UdSchoolService;
/**
 * 卡券管理
 * @author mutaotao
 *
 */
@IocBean
@At("/platform/card")
public class UdCardController {

	@Inject
	private UdCardService cardService;
	@Inject
	private UdCompanyService companyService;
	@Inject
	private UdRegionService regionService;
	@Inject
	private UdSchoolService schoolService;
	@Inject
	private UdProductTypeService productTypeService;
	
	/**
	 * 首页
	 */
	@At("")
	@Ok("beetl:/platform/card/index.html")
	@RequiresAuthentication
	public void index() {

	}
	/**
	 * 加载数据
	 * @param title
	 * @param length
	 * @param start
	 * @param draw
	 * @param order
	 * @param columns
	 * @return
	 */
	@At
	@Ok("json:full") 
	@RequiresAuthentication
	public Object data( @Param("title") String title,@Param("length") int length, @Param("start") int start,
			@Param("draw") int draw, @Param("::order") List<DataTableOrder> order,
			@Param("::columns") List<DataTableColumn> columns) {
		Subject subject = SecurityUtils.getSubject();
		if (subject != null) {
			Cnd cnd = Cnd.NEW();
			//获取主体
			Sys_user user = (Sys_user) subject.getPrincipal();
			String company_id=user.getUnit().getCompany_id();
			cnd.and("company_id", "=", company_id);
			if (!Strings.isBlank(title))
				cnd.and("title", "like", "%" + title + "%");
			return cardService.data(length, start, draw, order, columns, cnd, null);
		}else{
			return Result.error("system.error");
		}
	}
	
	/**
	 * 投放
	 * @return
	 */
	@At("/putOn/?")
	@Ok("beetl:/platform/card/puton.html")
	@RequiresAuthentication
	public Object putOn(String id){
		Map<String,Object> map=new HashMap<String,Object>();
	/*	List<Ud_company> companyList=companyService.find();
		map.put("companyList", companyList);*/
		List<Ud_region> regionList=regionService.find();
		map.put("regionList", regionList);
		List<Ud_school> schoolList=schoolService.find();
		map.put("schoolList", schoolList);
		map.put("card", cardService.fetch(id));
		//指定产品类型
		List<Ud_product_type> productList=productTypeService.find();
		map.put("productList", productList);
		return map;
	}
	
	@At("/putonDo")
	@Ok("json")
	@RequiresAuthentication 
	public Object putonDo(@Param("id") String id,@Param("spec_product_type") String spec_product_type,@Param("spec_region") String spec_region,@Param("spec_school") String spec_school,@Param("specs") String specs,HttpServletRequest request) {
		Ud_card card=cardService.fetch(id);
		if (card != null) {
			try{
				card.setState(1);
				card.setSpec_school(spec_school);
				card.setSpec_region(spec_region);
				card.setSpec_product_type(spec_product_type);
				card.setSpecs(specs);
				cardService.update(card);
				return Result.success("system.success");
			}catch (Exception e) {
				return Result.error("system.error");
			}
		}
		return Result.error("system.error");
	}
	
	
	@At
	@Ok("json") 
	@RequiresAuthentication
	public Object getCompanyName(String id){
		return companyService.fetch(id).getName();
	}
	
	@At
	@Ok("json") 
	@RequiresAuthentication
	public Object getCardName(String rawid,String source){
		return cardService.getCardName(rawid,source);
	}
	
}
